package com.yangaobo.mapper;

import com.yangaobo.dto.StudentPageDTO;
import com.yangaobo.entity.Student;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface StudentMapper {
    @Insert("""
            insert into cms_student(`number`, `realname`, `avatar`, `phone`, `gender`, `wechat`, `age`, `province`, `address`, `fk_school_id`, `fk_salesman_id`, `fk_club_id`, `idcard`, `study_date`, `emergency_name`, `emergency_phone`, `status`, `info`, `deleted`, `version`, `created`, `updated`)
            values (#{number}, #{realname}, #{avatar}, #{phone}, #{gender}, #{wechat}, #{age}, #{province}, #{address}, #{fkSchoolId}, #{fkSalesmanId}, #{fkClubId}, #{idcard}, #{studyDate}, #{emergencyName}, #{emergencyPhone}, #{status}, #{info}, #{deleted}, #{version}, #{created}, #{updated})
            """)
    int insert(Student student);

    @Results(id = "studentResultMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "fkSchoolId", column = "fk_school_id"),
            @Result(property = "fkSalesmanId", column = "fk_salesman_id"),
            @Result(property = "fkClubId", column = "fk_club_id"),
            @Result(property = "school", column = "fk_school_id", one = @One(select = "com.yangaobo.mapper.SchoolMapper.select")),
            @Result(property = "salesman", column = "fk_salesman_id", one = @One(select = "com.yangaobo.mapper.EmpMapper.select")),
            @Result(property = "club", column = "fk_club_id", one = @One(select = "com.yangaobo.mapper.ClubMapper.select")),
    })
    @Select("""
            select * from cms_student t
            where t.id = #{param1} and t.deleted = 0
            """)
    Student select(Long id);

    @ResultMap("studentResultMap")
    @Select("""
            <script>
            select * from cms_student t
            <where>
                <if test='number != null'> number like concat('%', #{number}, '%') and </if>
                <if test='realname != null'> realname like concat('%', #{realname}, '%') and </if>
                <if test='phone != null'> phone = #{phone} and </if>
                <if test='fkSchoolId != null'> fk_school_id = #{fkSchoolId} and </if>
                <if test='fkSalesmanId != null'> fk_salesman_id = #{fkSalesmanId} and </if>
                <if test='fkClubId != null'> fk_club_id = #{fkClubId} and </if>
                <if test='status != null'> status = #{status} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Student> list(StudentPageDTO dto);

    @Update("""
            <script>
            update cms_student
            <set>
                <if test="number != null">number = #{number},</if>
                <if test="realname != null">realname = #{realname},</if>
                <if test="avatar != null">avatar = #{avatar},</if>
                <if test="phone != null">phone = #{phone},</if>
                <if test="gender != null">gender = #{gender},</if>
                <if test="wechat != null">wechat = #{wechat},</if>
                <if test="age != null">age = #{age},</if>
                <if test="province != null">province = #{province},</if>
                <if test="address != null">address = #{address},</if>
                <if test="fkSchoolId != null">fk_school_id = #{fkSchoolId},</if>
                <if test="fkSalesmanId != null">fk_salesman_id = #{fkSalesmanId},</if>
                <if test="fkClubId != null">fk_club_id = #{fkClubId},</if>
                <if test="idcard != null">idcard = #{idcard},</if>
                <if test="studyDate != null">study_date = #{studyDate},</if>
                <if test="emergencyName != null">emergency_name = #{emergencyName},</if>
                <if test="emergencyPhone != null">emergency_phone = #{emergencyPhone},</if>
                <if test="status != null">status = #{status},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
            )
    int update(Student student);

    @Update("""
            update cms_student set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update cms_student set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

}
